/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apps.epocket.stripes.action;

import able.stripes.JSP;
import com.apps.epocket.manager.MailManager;
import com.apps.epocket.manager.PengiklananManager;
import com.apps.epocket.model.DokumenPengiklanan;
import com.apps.epocket.model.MailBean;
import com.apps.epocket.model.Pengiklanan;
import com.apps.epocket.stripes.BaseActionBean;
import com.apps.epocket.stripes.Configuration;
import com.apps.epocket.stripes.StatusConstraints;
import com.apps.epocket.stripes.util.FileUtil;
import com.google.inject.Inject;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.*;

/**
 *
 * @author fikri
 */
@Wizard(startEvents = {"Step1"})
@UrlBinding("/advertisment")
public class AdsActionBean extends BaseActionBean {

    private Pengiklanan ads;
    
    private Integer idAds;
    
    private List<FileBean> senaraiDokumenIklan;
    
    @Inject
    private Configuration conf;
    
    @Inject
    PengiklananManager pengiklananManager;
    
    @Inject MailManager mailManager;

    @HandlesEvent("Step1")
    @DefaultHandler
    public Resolution showForm() {
        return new JSP("ads/ads_insert.jsp");
    }

    @HandlesEvent("Step2")
    public Resolution viewForm() {
        ads = pengiklananManager.findById(idAds);
        getContext().getRequest().setAttribute("title", ads.getNamaIklan());
        return new JSP("ads/ads_view.jsp");
    }

    @HandlesEvent("Step3")
    public Resolution confirm() {

        List<DokumenPengiklanan> senaraiDokumen = new ArrayList<DokumenPengiklanan>();

        if (senaraiDokumenIklan != null && !senaraiDokumenIklan.isEmpty()) {

            FileUtil fileUtil = new FileUtil();

            String dms = conf.getProperty("dms.path");

            String fileDms = ads.getNoKp() + File.separator + new SimpleDateFormat("yyyyMMdd").format(new Date());

            String fullDmsPath = dms + (dms.endsWith(File.separator) ? "" : File.separator) + fileDms;

            for (FileBean file : senaraiDokumenIklan) {

                if (file == null) {
                    continue;
                }

                String filePath = "";

                try {
                    filePath = fileUtil.saveFile(fullDmsPath, file.getFileName(), file.getInputStream());
                    DokumenPengiklanan dokumenPengiklanan = new DokumenPengiklanan();
                    dokumenPengiklanan.setPengiklanan(ads);
                    dokumenPengiklanan.setFizikalLokasi(fileDms + File.separator + file.getFileName());
                    dokumenPengiklanan.setFormat(file.getContentType());
                    senaraiDokumen.add(dokumenPengiklanan);
                } catch (Exception ex) {
                }
            }
        }
        ads.setTrhMasuk(new Date());
        ads.setStatusPengiklanan(StatusConstraints.PENDING_ADS);
        pengiklananManager.save(ads, senaraiDokumen);


        MailBean bean = new MailBean();
        bean.setMailFrom("donotreply@pocket.my");
        bean.setMailTo("nimda1902@gmail.com");        
        bean.setTitle("new ads request!!");
        bean.setContent(getContent());
        mailManager.send(bean);


        return new JSP("ads/ads_notification.jsp");
    }
    
    
    String getContent(){
        StringBuilder sb = new StringBuilder();
        sb.append("Dear Sir \n\n");
        sb.append("New Ads Request received.\n\n");        
        sb.append("Please Login into the system to validate the ads.\n\n\n");
        sb.append("If you have any enquiries, please do not hesitate to contact us at enquiry@pocket.my\n\n\n");
        sb.append("Thank You\n");
        sb.append("Yours Sincerely\n\n");
        sb.append("pocket.my\n\n\n");        
        sb.append("This email is auto generated.");
        return sb.toString();
    }

    public Pengiklanan getAds() {
        return ads;
    }

    public void setAds(Pengiklanan ads) {
        this.ads = ads;
    }

    public List<FileBean> getSenaraiDokumenIklan() {
        return senaraiDokumenIklan;
    }

    public void setSenaraiDokumenIklan(List<FileBean> senaraiDokumenIklan) {
        this.senaraiDokumenIklan = senaraiDokumenIklan;
    }

    public Integer getIdAds() {
        return idAds;
    }

    public void setIdAds(Integer idAds) {
        this.idAds = idAds;
    }
}
